package com.awccis.customer.service.impl;

import javax.annotation.Resource;

import com.awccis.admin.model.sorder.ServiceOrderSimpleBean;
import com.awccis.common.utils.Constants;
import com.awccis.common.utils.HttpRequestUtil;
import com.awccis.common.utils.JsonUtil;
import com.awccis.common.utils.SendMessageUtil;
import com.awccis.customer.model.UsualAddr;
import com.awccis.customer.model.adver.AdverImgLocationBean;
import com.awccis.customer.model.order.AppraiseBean;
import com.google.gson.JsonObject;
import org.springframework.stereotype.Service;

import com.awccis.customer.dao.CustomerDao;
import com.awccis.customer.dao.CustomerOrderDao;
import com.awccis.customer.model.CustomerInfo;
import com.awccis.customer.model.order.OrderBean;
import com.awccis.customer.model.order.OrderInfo;
import com.awccis.customer.service.CustomerService;

import java.io.*;
import java.util.*;

/**
 * @Author: 纯牛奶丶
 * @Date: 2018-04-03 21:43
 * @Since: 1.0
 * @Description: 乘客端业务逻辑接口实现类
 */
@Service("CustomerServiceImpl")
public class CustomerServiceImpl implements CustomerService {
    @Resource
    private CustomerDao      customerDao;
    @Resource
    private CustomerOrderDao customerOrderDao;

    /**
     * 计算价钱
     *
     * @param distance 距离
     * @return 价钱
     */
    @Override
    public double calculatePrice(double distance) {
        return customerOrderDao.callPreGetSum(distance);
    }

    /**
     * 取消订单
     *
     * @param orderInfo orderInfo
     * @return boolean
     */
    @Override
    public boolean cancelOrder(OrderInfo orderInfo) {
        return customerOrderDao.deleteOrderByOrderId(orderInfo) == 1;
    }
    
    /**查询评价
     * @param orderId orderId
     * @return boolean
     */
    @Override
    public AppraiseBean selectAppraiseOrder(Integer orderId) {
        return customerDao.selectAppraise(orderId);
    }
    
    /**
     * 评价
     *
     * @param appraiseBean appraiseBean
     * @return boolean
     */
    @Override
    public boolean insertAppraiseOrder(AppraiseBean appraiseBean) {
        boolean flag;
        AppraiseBean appraiseBean1 = selectAppraiseOrder(appraiseBean.getOrderId());
        if (appraiseBean1 == null) {
            flag = customerDao.insertAppraise(appraiseBean) == 1;
        } else {
            flag = false;
        }
        return flag;
    }
    
    /**
     * 查询常用地址信息
     * @return Map<Integer,List<UsualAddr>>
     */
    @Override
    public Map<Integer,List<UsualAddr>> selectUsualAddr() {
        List<UsualAddr> usualAddrs = customerDao.selectUsualAddr();
        Map<Integer, List<UsualAddr>> map = new HashMap<>();
        List<UsualAddr> schoolAddrs=new ArrayList<>();
        List<UsualAddr> businessAddrs=new ArrayList<>();
        List<UsualAddr> trafficAddrs=new ArrayList<>();
        List<UsualAddr> amusementAddrs=new ArrayList<>();
        //对查询到的结果集进行分类
        for (UsualAddr usualAddr : usualAddrs) {
            switch (usualAddr.getAddrType()) {
                case 1:
                    schoolAddrs.add(usualAddr);
                    break;
                case 2:
                    businessAddrs.add(usualAddr);
                    break;
                case 3:
                    trafficAddrs.add(usualAddr);
                    break;
                case 4:
                    amusementAddrs.add(usualAddr);
                    break;
                default:
                        break;
            }
        }
        map.put(1, schoolAddrs);
        map.put(2, businessAddrs);
        map.put(3, trafficAddrs);
        map.put(4, amusementAddrs);
        
        return map;
    }
    
    /**
     * 根据地址名称返回地址详细信息、经纬度
     * @param addrName addrName
     * @return List<UsualAddr>
     */
    @Override
    public UsualAddr selectUsualAddrByName(String addrName) {
        return customerDao.selectUsualAddrByName(addrName);
    }
    
    /**
     * 添加初始订单
     *
     * @param orderBean orderBean
     * @return boolean
     */
    @Override
    public int insertOrderInfoInit(OrderBean orderBean) {
        orderBean.setOrderStartTime(new Date());
        customerDao.insertOrderInfoInit(orderBean);
        return  orderBean.getOrderId();
    }
    
    /**
     * 查询历史订单总数
     * @param map customerInfo
     * @return List<Map>
     */
    @Override
    public int selectHistoryOrderCount(Map map) {
        return customerDao.selectHistoryOrderCount(map);
    }
    /**
     * 查询历史订单
     * @param map customerInfo
     * @return List<Map>
     */
    @Override
    public List<Map> selectHistoryOrder(Map map) {
        return customerDao.selectHistoryOrder(map);
    }
    
    /**
     * 乘客端信息注册
     *
     * @param customerInfo customerInfo
     * @return int
     */
    @Override
    public boolean registerCustomer(CustomerInfo customerInfo) {
        return customerDao.insertCustomerInfo(customerInfo) == 1;
    }

    /**
     * 更改乘客信息
     *
     * @param customerInfo customer
     * @return
     */
    @Override
    public Boolean updateCustomerInfo(CustomerInfo customerInfo) {
        return customerDao.updateCustomerInfoByPhone(customerInfo) == 1;
    }

    /**
     * 乘客验证并登陆，获得个人信息
     *
     * @param customerInfo customerInfo
     * @return customerInfo
     */
    @Override
    public CustomerInfo getCustomerLoginInfo(CustomerInfo customerInfo) {
        return customerDao.selectCustomerInfo(customerInfo);
    }
    
//    /**
//     * 查询是否已登录
//     * 登陆：0
//     * 未登录：1
//     *
//     * @param customerPhone customerPhone
//     * @return boolean
//     */
//    @Override
//    public boolean selectLoginStatus(String customerPhone) {
//        return customerDao.selectLoginStatus(customerPhone) == 0;
//    }
    
//    /**
//     *  设置登录状态为online
//     * @param customerPhone customerInfo
//     * @return customerInfo
//     */
//    @Override
//    public Integer setOnline(String customerPhone) {
//
//        return customerDao.setOnline(customerPhone);
//    }
//    /**
//     *  设置登录状态为Offline
//     * @param customerInfo customerInfo
//     * @return customerInfo
//     */
//    @Override
//    public Integer setOffline(CustomerInfo customerInfo) {
//
//        return customerDao.setOffline(customerInfo);
//    }
    
//    /**
//     * 获取前10相对距离靠前的司机
//     *
//     * @param map
//     * @return 司机列表
//     */
//    @Override
//    public List<DriverInfo> getDriverTop10(Map map) {
//        return customerDao.selectDriverTop10(map);
//    }

    /**
     * 通过phone获取当前的订单
     *
     * @param hashMap customerPhone
     * @return orderBean
     */
    @Override
    public OrderBean selectCurrentOrderByPhone(HashMap hashMap) {
        return customerOrderDao.selectCurrentOrder(hashMap);
    }

    /**
     * 获取图片位置
     *
     * @return list
     */
    @Override
    public List<AdverImgLocationBean> getAdverImgPath() {
        return customerDao.selectCurrentLogoPath();
    }

    /**
     * 从数据库中t_logo表随机获取一个广告图片
     * @return list
     */
    @Override
    public List<AdverImgLocationBean> getOneRandomLogoPath() {
        return customerDao.selectOneRandomLogoPath();
    }

    /**
     * 随机获取一张广告图片
     * @return list
     */
    @Override
    public List<AdverImgLocationBean> getOneRandomLogoPathByCode() {
        List<AdverImgLocationBean> adverImgLocationBeans = customerDao.selectAllLogoPath();
        List<AdverImgLocationBean> list = new ArrayList<>();
        list.add(adverImgLocationBeans.get(((int) (Math.random() * adverImgLocationBeans.size()))));
        return list;
    }

    /**
     * 给司机发送订单信息短息
     * @param orderBean orderBean
     */
    @Override
    public String sendOrderInfoToDriver(OrderBean orderBean){

        String info = "E加家 平台信息" + "\n";
        String from = "载客地址: " + orderBean.getOrderTravelOnPlace()+"\n";
        String to = "目的地: "+orderBean.getOrderTravelEndPlace()+"\n";
        String phone = "联系电话: "+orderBean.getCustomerPhone()+"\n";

        String message = "AWCCIS\n" + info + from + to + phone;
        String driverPhone = orderBean.getDriverPhone();

        return SendMessageUtil.sendMessageTo(driverPhone, message);
    }

    /**
     * 根据ID查询乘客的预约订单
     *
     * @param customerId customerID
     * @return list
     */
    @Override
    public List<ServiceOrderSimpleBean> getCusotmerServiceOrderById(int customerId) {
        return customerOrderDao.selectDealtServiceOrderById(customerId);
    }

    /**
     * 记录访问信息
     * @param map 1
     */
    @Override
    public void recordVisitInfo(Map map) {
        try {
            String path = JsonUtil.getJsonConfigValue("visitInfoLogPath");
            if (!new File(path).exists()) {
                new File(path).mkdirs();
            }
            File file = new File(path, "customerVisitInfo.log");
            
            BufferedWriter br = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file,true)));
            br.write(String.valueOf(map.get("date")) + "," + map.get("os") + "," + map.get("model") + ","+map.get("ip")+"\n" );
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据订单号查询对应司机编号
     *
     * @param orderId
     */
    @Override
    public int getDriverIdByOrderId(int orderId) {
        try {
            return customerOrderDao.selectDriverIdByOrderId(orderId);
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }
}

